package com.itheima.package4_tool;

import java.util.concurrent.Semaphore;

public class MySemaphoreDemo {
    public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
        for (int i = 0; i < 100; i++) {
            new Thread(mr).start();
        }
    }
}
class MyRunnable implements Runnable{
private Semaphore sp = new Semaphore(2);
    @Override
    public void run() {
        try {
            sp.acquire();
            System.out.println("获得通行证，开始行驶");
            System.out.println("归还通行证");
            sp.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}